package string_question.leecode.test6;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 17:41
 */
public class Solution3 {

    // 方法三： 针对方法一在代码上面的优化
    public String convert(String s, int numRows) {
        if (s == null || s.length() < 3 || numRows == 1) return s;
        StringBuilder ans = new StringBuilder();
        char[] chars = s.toCharArray();
        int n = chars.length;
        int step = 2 * (numRows - 1);
        int c = 0;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j + i < n; j += step) {
                ans.append(chars[j + i]);
                if (i != 0 && i != numRows - 1 && j - i + step < n) {
                    ans.append(chars[j + step - i]);
                }
            }
        }
        return ans.toString();
    }
}
